<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="并发非阻塞缓存">
<meta name="twitter:description" content="<p>竞态检测器；并发非阻塞缓存：串行版本，使用互斥锁，重复抑制，另一种实现-使用监控goroutine；小结：CSP并发模型</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/gopl-concurrent-non—blocking-cache.html"/>
<meta property="og:title" content="来玩魔王的咚 | 并发非阻塞缓存" />
<meta property="og:description" content="<p>竞态检测器；并发非阻塞缓存：串行版本，使用互斥锁，重复抑制，另一种实现-使用监控goroutine；小结：CSP并发模型</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>竞态检测器；并发非阻塞缓存：串行版本，使用互斥锁，重复抑制，另一种实现-使用监控goroutine；小结：CSP并发模型</p>" />

    <meta name="tags" content="gopl" />
    <meta name="tags" content="sync" />
    <meta name="tags" content="测试" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li class="active"><a href="/category/go.html">Go</a></li>
                            <li><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-08-09 10:00:00+08:00">
                <script>document.write(moment('2020-08-09 10:00:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-08-09 10:00:00+08:00">
                <script>document.write(moment('2020-08-10 10:00:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            并发非阻塞缓存
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#jing-tai-jian-ce-qi">竞态检测器</a><ul>
<li><a href="#kai-qi-jing-tai-jian-ce-qi">开启竞态检测器</a></li>
<li><a href="#ru-he-jian-cha-dao-jing-tai">如何检查到竞态</a></li>
<li><a href="#nei-xie-jing-tai-ke-neng-cha-bu-dao">哪些竞态可能查不到</a></li>
<li><a href="#ke-yi-zai-sheng-chan-huan-jing-kai-qi-jing-tai-jian-ce-qi">可以在生产环境开启竞态检测器</a></li>
</ul>
</li>
<li><a href="#shi-li-bing-fa-fei-zu-se-huan-cun">示例：并发非阻塞缓存</a><ul>
<li><a href="#bei-huan-cun-jie-guo-de-han-shu">被缓存结果的函数</a></li>
<li><a href="#chuan-xing-de-ban-ben">串行的版本</a></li>
<li><a href="#shi-yong-hu-chi-suo">使用互斥锁</a></li>
<li><a href="#chong-fu-yi-zhi">重复抑制</a></li>
<li><a href="#ling-yi-zhong-shi-xian-shi-yong-jian-kong-goroutine">另一种实现-使用监控goroutine</a></li>
<li><a href="#xiao-jie">小结</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="jing-tai-jian-ce-qi"><a class="toclink" href="#jing-tai-jian-ce-qi">竞态检测器</a></h3>
<p>即使再仔细的检查，仍然可能在并发上犯错。Go 的 runtime 提供了动态分析工具：<strong>竞态检测器</strong>（race detectotr）。<br>
在下一节的示例中会用到竞态检测器，所以在用之前，先了解一下这个工具。  </p>
<h4 id="kai-qi-jing-tai-jian-ce-qi"><a class="toclink" href="#kai-qi-jing-tai-jian-ce-qi">开启竞态检测器</a></h4>
<p>简单地把 -race 命令行参数加到 go build、go run、go test 命令里即可使用该功能。它会让编译器为你的应用或测试构建一个修改后的版本，这个版本有额外的手法可以高效记录在执行时对共享变量的所有访问，以及读写这些变量的 goroutine 标识。除此之外，还会记录所有的同步事件、包括 go 语句、通道操作、锁的调用等。（完整的同步事件集合可以在语言规范中的 “The Go Memory Model” 文档中找到。）  </p>
<h4 id="ru-he-jian-cha-dao-jing-tai"><a class="toclink" href="#ru-he-jian-cha-dao-jing-tai">如何检查到竞态</a></h4>
<p>竞态检测器会研究事件流，找到那些有问题的案例，即一个 goroutine 写入一个变量后，中间没有任何同步的操作，就有另外一个 goroutine 读写了该变量。这种情况表明有对共享变量的并发访问，即数据竞态。工具会输出一份报告，包括变量的标识以及读写 goroutine 当时的调用栈。通常情况下这些信息足以定位问题了，下一章的示例会应用到实战中。  </p>
<h4 id="nei-xie-jing-tai-ke-neng-cha-bu-dao"><a class="toclink" href="#nei-xie-jing-tai-ke-neng-cha-bu-dao">哪些竞态可能查不到</a></h4>
<p>竞态检测器报告所有实际运行了的数据竞态。但只能检测到那些在运行时发生的竞态，无法用来保证肯定不发生竞态。所以为了保证效果，需要全部测试包含了并发调用的场景。  </p>
<h4 id="ke-yi-zai-sheng-chan-huan-jing-kai-qi-jing-tai-jian-ce-qi"><a class="toclink" href="#ke-yi-zai-sheng-chan-huan-jing-kai-qi-jing-tai-jian-ce-qi">可以在生产环境开启竞态检测器</a></h4>
<p>由于存在额外的记录工作，带竞态检测功能的程序在执行时需要更长的时间和更多的内存，但即使对于生成环境的任务，这种额外开支也是可以接受的。对于那些偶发的竞态条件，使用竞态检测器可以节省很多调试的时间。  </p>
<h3 id="shi-li-bing-fa-fei-zu-se-huan-cun"><a class="toclink" href="#shi-li-bing-fa-fei-zu-se-huan-cun">示例：并发非阻塞缓存</a></h3>
<p>创建一个<strong>并发非阻塞的缓存</strong>系统，它能解决<strong>函数记忆</strong>（memoizing）的问题，即缓存函数的结果，达到多次调用但只须计算一次结果。这个问题在并发实战中很常见但已有的库不能很好地解决这个问题。这里的解决方案将会是并发安全的，并且要避免简单地对整个缓存使用单个锁而带来的锁争夺问题。  </p>
<h4 id="bei-huan-cun-jie-guo-de-han-shu"><a class="toclink" href="#bei-huan-cun-jie-guo-de-han-shu">被缓存结果的函数</a></h4>
<p>在做系统之前，先准备一个将要被测试的函数。这里将使用下面的 httpGetBody 函数作为示例来演示函数记忆。<br>
调用 HTTP 请求相当昂贵，所以我希望只在第一次请求的时候去发起请求，而之后都可以在缓存中找到结果直接返回：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">httpGetBody</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">resp</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">http</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadAll</span><span class="p">(</span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>先保证能缓存这个函数的执行结果，之后再使用更多个函数来测试和验证功能。  </p>
<h4 id="chuan-xing-de-ban-ben"><a class="toclink" href="#chuan-xing-de-ban-ben">串行的版本</a></h4>
<p>这是一个并发不安全的版本，不过把基本功能先实现，并发安全的问题之后再进行优化：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// memo包提供了一个对类型 Func 并发不安全的函数记忆功能</span><span class="w"></span>
<span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="c1">// Memo 缓存了调用 Func 的结果</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Memo</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="w">     </span><span class="nx">Func</span><span class="w"></span>
<span class="w">    </span><span class="nx">cache</span><span class="w"> </span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="nx">result</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Func 是用于记忆的函数类型</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Func</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">value</span><span class="w"> </span><span class="kd">interface</span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="nx">err</span><span class="w">   </span><span class="kt">error</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">Memo</span><span class="p">{</span><span class="nx">f</span><span class="p">:</span><span class="w"> </span><span class="nx">f</span><span class="p">,</span><span class="w"> </span><span class="nx">cache</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="nx">result</span><span class="p">)}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// 注意：并发不安全</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">res</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>Memo 实例包含了被记忆的函数 f （类型为Func），以及缓存，类型为一个 key 为字符串，value 为 result 的 map。每个 result 都是调用 f 产生的结果：一个值和一个错误，在设计的推进过程中会展示 Memo 的几种变体，但所有变体都会遵守这些基本概念。  </p>
<h5 id="chuan-xing-ce-shi"><a class="toclink" href="#chuan-xing-ce-shi">串行测试</a></h5>
<p>下面的例子展示了如何使用 Memo。下面是完整的测试源码文件，包括上一小节写的被测试的函数，以及一串 URL。每个 URL 会发起两次请求。<br>
对于每个 URL，首先调用 Get，打印延时和返回的数据长度：</p>
<div class="highlight"><pre><span></span><code><span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="kn">import</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;io/ioutil&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;log&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;net/http&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;sync&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;testing&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;time&quot;</span><span class="w"></span>
<span class="p">)</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">httpGetBody</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">resp</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">http</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadAll</span><span class="p">(</span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">var</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;http://docscn.studygolang.com/&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://studygolang.com/&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://studygolang.com/pkgdoc&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://github.com/adonovan/gopl.io/tree/master/ch9&quot;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestSequential</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 串行</span><span class="w"></span>
<span class="w">    </span><span class="nx">m</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">httpGetBody</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="nx">urls</span><span class="p">,</span><span class="w"> </span><span class="nx">urls</span><span class="o">...</span><span class="p">)</span><span class="w"> </span><span class="c1">// 每个 URL 请求两次</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">start</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">log</span><span class="p">.</span><span class="nx">Print</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;%s, %s, %d bytes\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Since</span><span class="p">(</span><span class="nx">start</span><span class="p">),</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">value</span><span class="p">.([]</span><span class="kt">byte</span><span class="p">)))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestConcurrent</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 并行</span><span class="w"></span>
<span class="w">    </span><span class="nx">m</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">httpGetBody</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="w"></span>
<span class="w">    </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="nx">urls</span><span class="p">,</span><span class="w"> </span><span class="nx">urls</span><span class="o">...</span><span class="p">)</span><span class="w"> </span><span class="c1">// 每个 URL 请求两次</span><span class="w"></span>
<span class="w">    </span><span class="nx">n</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nx">urls</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">defer</span><span class="w"> </span><span class="nx">n</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="nx">start</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nx">log</span><span class="p">.</span><span class="nx">Print</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;%s, %s, %d bytes\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Since</span><span class="p">(</span><span class="nx">start</span><span class="p">),</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">value</span><span class="p">.([]</span><span class="kt">byte</span><span class="p">)))</span><span class="w"></span>
<span class="w">        </span><span class="p">}(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">n</span><span class="p">.</span><span class="nx">Wait</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>这里使用 testing 包系统的测试效果。上面有两个测试函数，先只用 TestSequential 进行测试，串行的发起请求。从下面的测试结果看，每一个 URL 第一次调用都会消耗一定的时间，但对 URL 第二次的请求会立刻返回结果：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\output\memo\memo1&gt; go test -run=TestSequential -v
=== RUN   TestSequential
http://docscn.studygolang.com/, 87.1978ms, 6612 bytes
https://studygolang.com/, 203.3312ms, 81819 bytes
https://studygolang.com/pkgdoc, 33.0053ms, 1261 bytes
https://github.com/adonovan/gopl.io/tree/master/ch9, 1.4428937s, 61185 bytes
http://docscn.studygolang.com/, 0s, 6612 bytes
https://studygolang.com/, 0s, 81819 bytes
https://studygolang.com/pkgdoc, 0s, 1261 bytes
https://github.com/adonovan/gopl.io/tree/master/ch9, 0s, 61185 bytes
--- PASS: TestSequential (1.81s)
PASS
ok      gopl/output/memo/memo1  2.063s
PS H:\Go\src\gopl\output\memo\memo1&gt;
</code></pre></div>

<p>默认在测试成功的时候不打印这类日志，不过可以加上 -v 参数在成功时也打印测试日志。  </p>
<h5 id="bing-xing-ce-shi"><a class="toclink" href="#bing-xing-ce-shi">并行测试</a></h5>
<p>这次测试中所有的 Get 都是串行的。因为 HTTP 请求通过并发来改善的空间很大，所以这次使用 TestConcurrent 进行测试，让所有的请求并发进行。这个测试要使用 sync.WaitGroup 等待所有的请求完成后再返回结果。<br>
这次的测试结果基本上都是缓存无效的情况，不过偶尔还会出现无法正常运行的情况。除了缓存无效，可能还会有缓存命中后返回错误结果，甚至崩溃：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\output\memo\memo1&gt; go test -run=TestConcurrent -v
=== RUN   TestConcurrent
http://docscn.studygolang.com/, 92.9972ms, 6612 bytes
http://docscn.studygolang.com/, 98.9889ms, 6612 bytes
https://studygolang.com/pkgdoc, 204.8383ms, 1261 bytes
https://studygolang.com/pkgdoc, 205.8387ms, 1261 bytes
https://studygolang.com/, 234.1566ms, 81819 bytes
https://studygolang.com/, 235.1749ms, 81819 bytes
https://github.com/adonovan/gopl.io/tree/master/ch9, 1.5041445s, 61184 bytes
https://github.com/adonovan/gopl.io/tree/master/ch9, 2.1051443s, 61184 bytes
--- PASS: TestConcurrent (2.11s)
PASS
ok      gopl/output/memo/memo1  2.346s
PS H:\Go\src\gopl\output\memo\memo1&gt;
</code></pre></div>

<h5 id="jia-shang-jing-tai-jian-ce-qi-jin-xing-bing-xing-ce-shi"><a class="toclink" href="#jia-shang-jing-tai-jian-ce-qi-jin-xing-bing-xing-ce-shi">加上竞态检测器进行并行测试</a></h5>
<p>更糟糕的是，多数时候这样都能正常运行，所以甚至很难注意到这样并发调用是有问题的。但是如果加上 -race 标志后再运行，那么竞态检测器就会输出如下的报告：</p>
<div class="highlight"><pre><span></span><code>PS H:\Go\src\gopl\output\memo\memo1&gt; go test -run=TestConcurrent -v -race
=== RUN   TestConcurrent
==================
WARNING: DATA RACE
Write at 0x00c000062cf0 by goroutine 11:
  runtime.mapassign_faststr()
      D:/Go/src/runtime/map_faststr.go:190 +0x0
  gopl/output/memo/memo1.(*Memo).Get()
      H:/Go/src/gopl/output/memo/memo1/memo.go:27 +0x1d8
  gopl/output/memo/memo1.TestConcurrent.func1()
      H:/Go/src/gopl/output/memo/memo1/memo_test.go:57 +0xc0

Previous write at 0x00c000062cf0 by goroutine 7:
  runtime.mapassign_faststr()
      D:/Go/src/runtime/map_faststr.go:190 +0x0
  gopl/output/memo/memo1.(*Memo).Get()
      H:/Go/src/gopl/output/memo/memo1/memo.go:27 +0x1d8
  gopl/output/memo/memo1.TestConcurrent.func1()
      H:/Go/src/gopl/output/memo/memo1/memo_test.go:57 +0xc0
...
FAIL    gopl/output/memo/memo1  2.883s
</code></pre></div>

<p>这里就是因为两个 goroutine 在没使用同步的情况下更新了 Memo.cache 这个 map。因为整个 Get 并不是并发安全的，它存在数据竞态：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// 注意：并发不安全</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">res</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>所以，接下来就是要改进，实现并发安全。  </p>
<h4 id="shi-yong-hu-chi-suo"><a class="toclink" href="#shi-yong-hu-chi-suo">使用互斥锁</a></h4>
<p>让缓存并发安全最简单的方法就是用一个基于监控的同步机制。需要给 Memo 加一个互斥量，并在 Get 开始就获取互斥锁，在返回前释放互斥锁，这样就可以让 cache 相关的操作发生在临界区域内了：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// Memo 缓存了调用 Func 的结果</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Memo</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="w">     </span><span class="nx">Func</span><span class="w"></span>
<span class="w">    </span><span class="nx">mu</span><span class="w">    </span><span class="nx">sync</span><span class="p">.</span><span class="nx">Mutex</span><span class="w"> </span><span class="c1">// 保护 cache</span><span class="w"></span>
<span class="w">    </span><span class="nx">cache</span><span class="w"> </span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="nx">result</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Get 是并发安全的</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Lock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">res</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>加上锁之后，再运行并发测试函数，竞态检测器不报警了。但是这次的修改后，之前对性能的优化就失效了。由于每次调用 Memo.f 时都上锁，所以现在的 Get 方法运行的使用实际又是串行的了。这里需要一个<strong>非阻塞的</strong>缓存，一个不会把他需要记忆的函数串行运行的缓存。<br>
调用 Get 是不需要锁保护的。调用 Get 的判断依据是之前的获取 map 的 key，这个操作需要加锁。调用 Get 返回后，需要把返回结果更新到 map 中去，这个操作也需要加锁。在 map 查询结束后，先释放锁。不加锁的情况下调用 Get。等到结果返回需要更新 map 的时候，再加锁更新 map。具体修改如下：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Lock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">ok</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">!</span><span class="nx">ok</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Lock</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">res</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>现在，可以安全的并行运行了，但是缓存又失效了。某些URL被获取了两次。修改一下测试源码文件的被测试函数 httpGetBody，在开头输出一行日志，可以观察到每个URL被调用的次数：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">httpGetBody</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">log</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;httpGetBody: %s&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">)</span><span class="w"> </span><span class="c1">// 输出哪些 url 被函数调用了，从缓存获取结果时不会有这个输出</span><span class="w"></span>
<span class="w">    </span><span class="nx">resp</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">http</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadAll</span><span class="p">(</span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>修改之后，可以用最初的串行版本再测试一下。那个版本是一定用到缓存的效果的。而现在的版本，在并发的情况下无法用上缓存。<br>
在几个 goroutine 几乎同时调用的 Get 来获取同一个 URL 时，每个 goroutine 都首先查询缓存，发现缓存中没有需要的数据，然后就都去执行 Get 来获取结果，最后又都用获得的结果来更新 map，其中一个结果会被另外一个覆盖。<br>
在理想的情况下，应该要避免这种额外的处理。这个功能有时称为<strong>重复抑制</strong>（duplicate suppression）。  </p>
<h4 id="chong-fu-yi-zhi"><a class="toclink" href="#chong-fu-yi-zhi">重复抑制</a></h4>
<p>下面这个版本，map 的每个元素是一个指向 entry 结构的指针。除了与之前一样包含一个已经记住的函数 f 调用结果之外，每个 entry 还新加一个通道 ready。<br>
在设置了 entry 和 result 字段后，通道会关闭，正在等待的 goroutine 会收到广播，然后就可以从 entry 字段读取结果了：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// memo包提供了一个对类型 Func 并发安全的函数记忆功能</span><span class="w"></span>
<span class="c1">// 并发、重复抑制、非阻塞的缓存</span><span class="w"></span>
<span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="kn">import</span><span class="w"> </span><span class="s">&quot;sync&quot;</span><span class="w"></span>

<span class="c1">// Func 是用于记忆的函数类型</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Func</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">value</span><span class="w"> </span><span class="kd">interface</span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="nx">err</span><span class="w">   </span><span class="kt">error</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="w">   </span><span class="nx">result</span><span class="w"></span>
<span class="w">    </span><span class="nx">ready</span><span class="w"> </span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}</span><span class="w"> </span><span class="c1">// res 准备好之后会被关闭</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">Memo</span><span class="p">{</span><span class="nx">f</span><span class="p">:</span><span class="w"> </span><span class="nx">f</span><span class="p">,</span><span class="w"> </span><span class="nx">cache</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="o">*</span><span class="nx">entry</span><span class="p">)}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">Memo</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">f</span><span class="w">     </span><span class="nx">Func</span><span class="w"></span>
<span class="w">    </span><span class="nx">mu</span><span class="w">    </span><span class="nx">sync</span><span class="p">.</span><span class="nx">Mutex</span><span class="w"> </span><span class="c1">// 保护 cache</span><span class="w"></span>
<span class="w">    </span><span class="nx">cache</span><span class="w"> </span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="o">*</span><span class="nx">entry</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Get 是并发安全的</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Lock</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">e</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// 对 key 的第一次访问，这个 goroutine 负责获取数据和广播数据准备好了的消息</span><span class="w"></span>
<span class="w">        </span><span class="nx">e</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">entry</span><span class="p">{</span><span class="nx">ready</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{})}</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">e</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span><span class="w"></span>

<span class="w">        </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="nb">close</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="p">)</span><span class="w"> </span><span class="c1">// 广播数据已经准备好的消息</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// 对这个 key 的重复访问</span><span class="w"></span>
<span class="w">        </span><span class="nx">memo</span><span class="p">.</span><span class="nx">mu</span><span class="p">.</span><span class="nx">Unlock</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="o">&lt;-</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="w"> </span><span class="c1">// 等待数据准备完毕</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>关于这里的 map 是否包含某个元素的判断，之前都是返回两个值，通过ok来判断。之前的示例中，map的元素是结构体，由于结构体类型的零值不是nil，通过ok来判断比较好。这里的元素类型是结构体指针，当然可以继续使用ok来判断。不过现在是指针类型了，零值是nil也不会和非零值的情况搞混，所以也可以直接通过nil来判断。<br>
现在调用 Get 会获取锁，然后去 map 中查询，如果没有找到，就直接分配并插入一个新的值，然后释放锁。之后其他 goroutine 来查询的时候，会发现值存在，那么就直接获取到 map 的值，然后释放锁。<br>
map 里的值并不是 Get 返回的数据，而是数据是否准备好的通道，和存放数据的字段。此时数据可能还没准备好，数据是否准备好，可以从 ready 通道进行判断。对 ready 通道的读取操作，会在数据没有准备好的时候一直阻塞。一旦数据准备好了，就会关闭 ready 通道，所有从 ready 通道的读取操作就会立刻返回。这是利用通道进行广播的方式。所以查询 map 后获取值的步骤就是先读取 ready 通道等待，一旦通道的读取返回，就表示数据已经准备好了，此时就可以去读取字段 res 里的内容并返回。<br>
注意，entry 中的变量 e.res.value 和 e.res.err 被多个 goroutine 共享。创建 entry 的 goroutine 会对这两个变量的值进行设置，其他 goroutine 在收到数据准备完毕的广播后才会开始读取这两个变量。尽管被多个 goroutine 访问，但是此处不需要加锁。ready 通道的关闭先于其他 goroutine 收到广播事件，所以第一个 goroutine 对变量的写入也先于后续多个 goroutine 的读取事件。这种情况下数据竞态不存在。<br>
到此，并发、重复抑制、非阻塞缓存就完成了。  </p>
<h4 id="ling-yi-zhong-shi-xian-shi-yong-jian-kong-goroutine"><a class="toclink" href="#ling-yi-zhong-shi-xian-shi-yong-jian-kong-goroutine">另一种实现-使用监控goroutine</a></h4>
<p>上面的示例是使用一个互斥量来保护 map 变量的并发安全。下面是另一种设计，让 map 变量限制在一个<strong>监控</strong> goroutine 中。<br>
首先是类型声明，New 函数在创建实例并返回的同时，还会启动一个 server 方法。该方法会集中处理所有的 Get 调用。我们在获取实例后，依然是调用 Get 来获取结果：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// memo包提供了一个对类型 Func 并发安全的函数记忆功能</span><span class="w"></span>
<span class="c1">// 并发、重复抑制、非阻塞的缓存</span><span class="w"></span>
<span class="c1">// 通过监控 goroutine 来实现并发安全</span><span class="w"></span>
<span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="c1">// Func 是用于记忆的函数类型</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Func</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">value</span><span class="w"> </span><span class="kd">interface</span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="nx">err</span><span class="w">   </span><span class="kt">error</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="w">   </span><span class="nx">result</span><span class="w"></span>
<span class="w">    </span><span class="nx">ready</span><span class="w"> </span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}</span><span class="w"> </span><span class="c1">// res 准备好之后会被关闭</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Func、result、entry 的声明和之前一致</span><span class="w"></span>

<span class="c1">// request 是一条请求消息</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">request</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">key</span><span class="w">      </span><span class="kt">string</span><span class="w">        </span><span class="c1">// 需要 Func 运行的参数</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="c1">// 每个客户端接收结果的通道</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">Memo</span><span class="w"> </span><span class="kd">struct</span><span class="p">{</span><span class="w"> </span><span class="nx">requests</span><span class="w"> </span><span class="kd">chan</span><span class="w"> </span><span class="nx">request</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">Memo</span><span class="p">{</span><span class="nx">requests</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="nx">request</span><span class="p">)}</span><span class="w"> </span><span class="c1">// 创建实例</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">server</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span><span class="w">                           </span><span class="c1">// 启动服务端 goroutine</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">memo</span><span class="w">                                 </span><span class="c1">// 返回实例，供客户端调用</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>可以先往后看客户端和服务端的处理逻辑，在回过来看这里声明的数据类型已经通道的作用。  </p>
<h5 id="ke-hu-duan"><a class="toclink" href="#ke-hu-duan">客户端</a></h5>
<p>现在 Get 就需要要给监控 goroutine 的通道发送请求和一个接收返回结果的通道。服务端会在收到处理请求后进行处理，之后再通过客户端发来的通道返回结果。<br>
而客户端发送请求之后，只需要从自己创建的这个通道中接收，直到接收到数据后，再返回即可：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="nx">result</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">request</span><span class="p">{</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">response</span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">response</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Close</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">close</span><span class="p">(</span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>客户端使用完之后，可以调用 Close 方法关闭发送请求的通道。  </p>
<h5 id="fu-wu-duan"><a class="toclink" href="#fu-wu-duan">服务端</a></h5>
<p>上面的 Get 相当于一个客户端，还需要一个服务端来处理 Get 发来的请求：</p>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">server</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">cache</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">req</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 一次处理收到的请求</span><span class="w"></span>
<span class="w">        </span><span class="nx">e</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">cache</span><span class="p">[</span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="c1">// 对这个 key 的第一次请求</span><span class="w"></span>
<span class="w">            </span><span class="nx">e</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">entry</span><span class="p">{</span><span class="nx">ready</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{})}</span><span class="w"></span>
<span class="w">            </span><span class="nx">cache</span><span class="p">[</span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">e</span><span class="w"></span>
<span class="w">            </span><span class="k">go</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span><span class="w"> </span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">)</span><span class="w"> </span><span class="c1">// 调用 f(key)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="c1">// 无论是否第一次请求，最后要回复结果，都有等待 ready 通道返回后，再去读取结果</span><span class="w"></span>
<span class="w">        </span><span class="k">go</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">deliver</span><span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">response</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"> </span><span class="nx">call</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">,</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 执行函数</span><span class="w"></span>
<span class="w">    </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 发送广播通知，数据已经准备好了</span><span class="w"></span>
<span class="w">    </span><span class="nb">close</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"> </span><span class="nx">deliver</span><span class="p">(</span><span class="nx">response</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">result</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 等待数据准备完毕</span><span class="w"></span>
<span class="w">    </span><span class="o">&lt;-</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 向客户端发送结果</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>变量 cache 被限制在监控 goroutine 中，就是上面的 server 方法。监控 goroutine 从 requests 的通道中读取请求，直到这个通道被关闭。对于每个请求，先查询缓存，如果没有找到就插入一个新的 entry。<br>
这里 call 和 deliver 方法需要在独立的 goroutine 中运行，以确保监控 goroutine 内持续处理新请求。  </p>
<h5 id="wan-zheng-shi-li-dai-ma"><a class="toclink" href="#wan-zheng-shi-li-dai-ma">完整示例代码</a></h5>
<p>下面贴上这个实现方式的完整代码：</p>
<div class="highlight"><pre><span></span><code><span class="c1">// memo包提供了一个对类型 Func 并发安全的函数记忆功能</span><span class="w"></span>
<span class="c1">// 并发、重复抑制、非阻塞的缓存</span><span class="w"></span>
<span class="c1">// 通过监控 goroutine 来实现并发安全</span><span class="w"></span>
<span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="c1">// Func 是用于记忆的函数类型</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">Func</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">value</span><span class="w"> </span><span class="kd">interface</span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="nx">err</span><span class="w">   </span><span class="kt">error</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">entry</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="w">   </span><span class="nx">result</span><span class="w"></span>
<span class="w">    </span><span class="nx">ready</span><span class="w"> </span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{}</span><span class="w"> </span><span class="c1">// res 准备好之后会被关闭</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Func、result、entry 的声明和之前一致</span><span class="w"></span>

<span class="c1">// request 是一条请求消息</span><span class="w"></span>
<span class="kd">type</span><span class="w"> </span><span class="nx">request</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">key</span><span class="w">      </span><span class="kt">string</span><span class="w">        </span><span class="c1">// 需要 Func 运行的参数</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">result</span><span class="w"> </span><span class="c1">// 每个客户端接收结果的通道</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">type</span><span class="w"> </span><span class="nx">Memo</span><span class="w"> </span><span class="kd">struct</span><span class="p">{</span><span class="w"> </span><span class="nx">requests</span><span class="w"> </span><span class="kd">chan</span><span class="w"> </span><span class="nx">request</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">Memo</span><span class="p">{</span><span class="nx">requests</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="nx">request</span><span class="p">)}</span><span class="w"> </span><span class="c1">// 创建实例</span><span class="w"></span>
<span class="w">    </span><span class="k">go</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">server</span><span class="p">(</span><span class="nx">f</span><span class="p">)</span><span class="w">                           </span><span class="c1">// 启动服务端 goroutine</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">memo</span><span class="w">                                 </span><span class="c1">// 返回实例，供客户端调用</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Close</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">close</span><span class="p">(</span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">Get</span><span class="p">(</span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="nx">result</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">request</span><span class="p">{</span><span class="nx">key</span><span class="p">,</span><span class="w"> </span><span class="nx">response</span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">res</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o">&lt;-</span><span class="nx">response</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">memo</span><span class="w"> </span><span class="o">*</span><span class="nx">Memo</span><span class="p">)</span><span class="w"> </span><span class="nx">server</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">cache</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">req</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">memo</span><span class="p">.</span><span class="nx">requests</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 一次处理收到的请求</span><span class="w"></span>
<span class="w">        </span><span class="nx">e</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">cache</span><span class="p">[</span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">e</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="c1">// 对这个 key 的第一次请求</span><span class="w"></span>
<span class="w">            </span><span class="nx">e</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="o">&amp;</span><span class="nx">entry</span><span class="p">{</span><span class="nx">ready</span><span class="p">:</span><span class="w"> </span><span class="nb">make</span><span class="p">(</span><span class="kd">chan</span><span class="w"> </span><span class="kd">struct</span><span class="p">{})}</span><span class="w"></span>
<span class="w">            </span><span class="nx">cache</span><span class="p">[</span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">e</span><span class="w"></span>
<span class="w">            </span><span class="k">go</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">f</span><span class="p">,</span><span class="w"> </span><span class="nx">req</span><span class="p">.</span><span class="nx">key</span><span class="p">)</span><span class="w"> </span><span class="c1">// 调用 f(key)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="c1">// 无论是否第一次请求，最后要回复结果，都有等待 ready 通道返回后，再去读取结果</span><span class="w"></span>
<span class="w">        </span><span class="k">go</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">deliver</span><span class="p">(</span><span class="nx">req</span><span class="p">.</span><span class="nx">response</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"> </span><span class="nx">call</span><span class="p">(</span><span class="nx">f</span><span class="w"> </span><span class="nx">Func</span><span class="p">,</span><span class="w"> </span><span class="nx">key</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 执行函数</span><span class="w"></span>
<span class="w">    </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="p">.</span><span class="nx">err</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">f</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 发送广播通知，数据已经准备好了</span><span class="w"></span>
<span class="w">    </span><span class="nb">close</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="w"> </span><span class="o">*</span><span class="nx">entry</span><span class="p">)</span><span class="w"> </span><span class="nx">deliver</span><span class="p">(</span><span class="nx">response</span><span class="w"> </span><span class="kd">chan</span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">result</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 等待数据准备完毕</span><span class="w"></span>
<span class="w">    </span><span class="o">&lt;-</span><span class="nx">e</span><span class="p">.</span><span class="nx">ready</span><span class="w"></span>
<span class="w">    </span><span class="c1">// 向客户端发送结果</span><span class="w"></span>
<span class="w">    </span><span class="nx">response</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nx">e</span><span class="p">.</span><span class="nx">res</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>针对上面的 memo 包的测试代码：</p>
<div class="highlight"><pre><span></span><code><span class="kn">package</span><span class="w"> </span><span class="nx">memo</span><span class="w"></span>

<span class="kn">import</span><span class="w"> </span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;io/ioutil&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;log&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;net/http&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;sync&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;testing&quot;</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;time&quot;</span><span class="w"></span>
<span class="p">)</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">httpGetBody</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="kd">interface</span><span class="p">{},</span><span class="w"> </span><span class="kt">error</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nx">log</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;httpGetBody: %s&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="nx">resp</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">http</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">ioutil</span><span class="p">.</span><span class="nx">ReadAll</span><span class="p">(</span><span class="nx">resp</span><span class="p">.</span><span class="nx">Body</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">var</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">string</span><span class="p">{</span><span class="w"> </span><span class="c1">// 换一批慢一些的网站，加载时间1s左右的国外资源</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://github.com/adonovan/gopl.io/tree/master/ch9&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://www.djangoproject.com/&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://getbootstrap.com/&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;https://www.python.org/&quot;</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestSequential</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 串行</span><span class="w"></span>
<span class="w">    </span><span class="nx">m</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">httpGetBody</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="nx">urls</span><span class="p">,</span><span class="w"> </span><span class="nx">urls</span><span class="o">...</span><span class="p">)</span><span class="w"> </span><span class="c1">// 每个 URL 请求两次</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nx">start</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nx">log</span><span class="p">.</span><span class="nx">Print</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;%s, %s, %d bytes\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Since</span><span class="p">(</span><span class="nx">start</span><span class="p">),</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">value</span><span class="p">.([]</span><span class="kt">byte</span><span class="p">)))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">func</span><span class="w"> </span><span class="nx">TestConcurrent</span><span class="p">(</span><span class="nx">t</span><span class="w"> </span><span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// 并行</span><span class="w"></span>
<span class="w">    </span><span class="nx">m</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">New</span><span class="p">(</span><span class="nx">httpGetBody</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">defer</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Close</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="kd">var</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="nx">sync</span><span class="p">.</span><span class="nx">WaitGroup</span><span class="w"></span>
<span class="w">    </span><span class="nx">urls</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="nx">urls</span><span class="p">,</span><span class="w"> </span><span class="nx">urls</span><span class="o">...</span><span class="p">)</span><span class="w"> </span><span class="c1">// 每个 URL 请求两次</span><span class="w"></span>
<span class="w">    </span><span class="nx">n</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nx">urls</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="nx">urls</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">go</span><span class="w"> </span><span class="kd">func</span><span class="p">(</span><span class="nx">url</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">defer</span><span class="w"> </span><span class="nx">n</span><span class="p">.</span><span class="nx">Done</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="nx">start</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Now</span><span class="p">()</span><span class="w"></span>
<span class="w">            </span><span class="nx">value</span><span class="p">,</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">m</span><span class="p">.</span><span class="nx">Get</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="nx">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">nil</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nx">log</span><span class="p">.</span><span class="nx">Print</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="nx">t</span><span class="p">.</span><span class="nx">Logf</span><span class="p">(</span><span class="s">&quot;%s, %s, %d bytes\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="nx">url</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">Since</span><span class="p">(</span><span class="nx">start</span><span class="p">),</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">value</span><span class="p">.([]</span><span class="kt">byte</span><span class="p">)))</span><span class="w"></span>
<span class="w">        </span><span class="p">}(</span><span class="nx">url</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="nx">n</span><span class="p">.</span><span class="nx">Wait</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<h4 id="xiao-jie"><a class="toclink" href="#xiao-jie">小结</a></h4>
<p>这里的例子展示了可以使用两种方案来构建并发结构：</p>
<ul>
<li>共享变量并上锁</li>
<li>通信顺序进程（communicating sequential process）</li>
</ul>
<p>第一种是大家普遍认知的，也是Java或者C++等语言中的多线程开发。<br>
第二种是 Go 语言特有的，也是 Go 语言推荐的。下面是一句推荐的原话：</p>
<blockquote>
<p>Do not communicate by sharing memory; instead, share memory by communicating.<br>
Go 箴言：“不要通过共享内存来通信，而应该通过通信来共享内存”。  </p>
</blockquote>
<p>在给定的情况下也许很难判定哪种方案更好，不过了解他们还是有价值的。有时候从一种方案切换到另外一种方案能让代码更简单。  </p>
<h5 id="csp-bing-fa-mo-xing"><a class="toclink" href="#csp-bing-fa-mo-xing">CSP并发模型</a></h5>
<p>CSP 是 Communicating Sequential Process 的简称，中文可以叫做通信顺序进程，是一种并发编程模型。<br>
CSP 模型由并发执行的实体（线程或者进程）所组成，实体之间通过发送消息进行通信，这里发送消息时使用的就是通道（channel）。CSP 模型的关键是关注 channel，而不关注发送消息的实体。Go 语言就是借用 CSP 模型的一些概念为之实现并发进行理论支持。Go 语言并没有完全实现 CSP 模型的所有理论，仅仅是借用了 process 和 channel 这两个概念。process 在 Go 语言上的表现就是 goroutine 是实际并发执行的实体，每个实体之间通过 channel 通讯来实现数据共享。  </p>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>